Imports System.Drawing
Imports Microsoft.Msagl.GraphViewerGdi
Imports Microsoft.Msagl.Drawing
Imports QuickGraph.Msagl
Imports QuickGraph
Imports System.Drawing.Text

Namespace Aricie.DNN.Caching.Analysis.Graphs
    Public Class GraphHelper



        Public Shared Function ToMsaglGraph(Of V, E As IEdge(Of V))(ByVal qGraph As IEdgeListGraph(Of V, E), _
                                                                    ByVal command As MsaglLayoutCommand(Of V, E)) As Graph

            Dim populator As MsaglGraphPopulator(Of V, E) = QuickGraph.Msagl.MsaglGraphExtensions.CreateMsaglPopulator(qGraph)



            AddHandler populator.NodeAdded, AddressOf command.MsaglVertexNodeEventHandler
            AddHandler populator.EdgeAdded, AddressOf command.MsaglEdgeNodeEventHandler

            populator.Compute()


            Dim toReturn As Graph = populator.MsaglGraph
            'toReturn.Attr.AspectRatio = 0.5

            'toReturn.Attr.NodeSeparation = 0
            'toReturn.Attr.LayerSeparation = 0
            'toReturn.Attr.MinNodeHeight = 30
            'toReturn.Attr.MinNodeWidth = 13.5
            toReturn.Attr.LayerDirection = LayerDirection.TB
            toReturn.Attr.BackgroundColor = Microsoft.Msagl.Drawing.Color.LightCyan
            Return toReturn

        End Function



        Public Shared Function ToBitmap(ByVal objGraph As Graph, ByVal width As Integer, ByVal height As Integer) As Bitmap


            Dim renderer As New GraphRenderer(objGraph)

            Dim bmp As New Bitmap(width, Convert.ToInt32(objGraph.Height * (width / objGraph.Width)), Imaging.PixelFormat.Format32bppArgb)
            Using gr As Graphics = Graphics.FromImage(bmp)
                gr.CompositingQuality = Drawing2D.CompositingQuality.Default
                gr.InterpolationMode = Drawing2D.InterpolationMode.High
                gr.SmoothingMode = Drawing2D.SmoothingMode.Default
                gr.PixelOffsetMode = Drawing2D.PixelOffsetMode.Default
                gr.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
                renderer.Render(gr, 0, 0, bmp.Width, bmp.Height)
            End Using
            Return bmp


        End Function



    End Class
End Namespace